home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993…ch: Other People's Memory / ADC Developer CD (1993-03) (''Other People's Memory'')_iso / Dev.CD Mar 93.iso / Technical Documentation / Sample Code / DTS.Lib & Samples / DTS.Lib / Init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-22  |  7.7 KB  |  238 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:         DTS.Lib
  5. ** File:            init.c
  6. ** Some code from:  Traffic Light 2.0 (2.0 version by Keith Rollin)
  7. ** Modified by:     Eric Soldan
  8. **
  9. ** Copyright © 1989-1991 Apple Computer, Inc.
  10. ** All rights reserved.
  11. */
  12.  
  13.  
  14.  
  15. /*****************************************************************************/
  16.  
  17.  
  18.  
  19. #include "DTS.Lib2.h"
  20. #include "DTS.Lib.Common.h"
  21. #include "DTS.Lib.protos.h"
  22.  
  23. #ifndef __ERRORS__
  24. #include <Errors.h>
  25. #endif
  26.  
  27. #ifndef __GESTALTEQU__
  28. #include <GestaltEqu.h>
  29. #endif
  30.  
  31. #include "Utilities.h"
  32.  
  33.  
  34.  
  35. /*****************************************************************************/
  36.  
  37.  
  38.  
  39. /* Set up the whole world, including global variables, Toolbox managers, and
  40. ** menus.  We also create our one application window at this time.  Since
  41. ** window storage is non-relocateable, how and when to allocate space for
  42. ** windows is very important so that heap fragmentation does not occur. */
  43.  
  44. /* The code that used to be part of ForceEnvirons has been moved into this
  45. ** module.  If an error is detected, instead of merely doing an ExitToShell,
  46. ** which leaves the user without much to go on, we call DeathAlert, which puts
  47. ** up a simple alert that just says an error occurred and then calls
  48. ** ExitToShell.  Since there is no other cleanup needed at this point if an
  49. ** error is detected, this form of error-handling is acceptable.  If more
  50. ** sophisticated error recovery is needed, an exception mechanism, such as is
  51. ** provided by Signals, can be used. */
  52.  
  53.  
  54.  
  55. /*****************************************************************************/
  56.  
  57.  
  58.  
  59. /* NOTE:  The “g” prefix is used to emphasize that a variable is global. */
  60.  
  61. Boolean    gQuitApplication;
  62.  
  63. extern Boolean        gHasAppleEvents, gNoDefaultDocument;
  64. extern RgnHandle    gCursorRgn;
  65. extern short        gPrintPage;
  66. extern SFTypeList    gTypeList;
  67.  
  68. typedef void (*callBack)(void);
  69.  
  70.  
  71.  
  72. /*****************************************************************************/
  73. /*****************************************************************************/
  74.  
  75.  
  76.  
  77. /* Given minHeap and minSpace values, get stuff going.  Also, we are passed
  78. ** in two procedure pointers.  If these are not nil, they are called at
  79. ** intermediate points during the initialization process.  The first proc
  80. ** is called after the Utilities.c standard initialization is complete.  The
  81. ** second proc is called very near the end of the initialization, but just
  82. ** prior to the menus being initialized. */
  83.  
  84. #pragma segment Initialize
  85. void    Initialize(short moreMasters, long minHeap, long minSpace, ProcPtr init1, ProcPtr init2)
  86. {
  87.     long    total, contig;
  88.  
  89.     StandardInitialization(moreMasters);
  90.  
  91.     if (init1)
  92.         (*(callBack)init1)();    /* Give app a chance to do some init stuff here. */
  93.  
  94.     /* Make sure that the machine has at least 128K ROMs.
  95.     ** If it doesn’t, exit. */
  96.  
  97.     if ((gMachineType < gestaltMac512KE) || (!gHasWaitNextEvent))
  98.         DeathAlert(rBadNewsStrings, sWimpyMachine);
  99.  
  100.     /* We used to make a check for memory at this point by examining
  101.     ** ApplLimit, ApplicZone, and StackSpace and comparing that to the minimum
  102.     ** size we told MultiFinder we needed.  This did not work well because it
  103.     ** assumed too much about the relationship between what we asked
  104.     ** MultiFinder for and what we would actually get back, as well as how to
  105.     ** measure it.  Instead, we will use an alternate method comprised of
  106.     ** two steps. */
  107.  
  108.     /* It is better to first check the size of the application heap against a
  109.     ** value that you have determined is the smallest heap the application can
  110.     ** reasonably work in.  This number should be derived by examining the
  111.     ** size of the heap that is actually provided by MultiFinder when the
  112.     ** minimum size requested is used.  The derivation of the minimum size
  113.     ** requested from MultiFinder is described in DTS.Lib.h.  The check should
  114.     ** be made because the preferred size can end up being set smaller than
  115.     ** the minimum size by the user.  This extra check acts to ensure that
  116.     ** your application is starting from a solid memory foundation. */
  117.  
  118.     if ((long)GetApplLimit() - (long)ApplicZone() < minHeap)
  119.         DeathAlert(rBadNewsStrings, sHeapTooSmall);
  120.  
  121.     /* Next, make sure that enough memory is free for your application to run.
  122.     ** It is possible for a situation to arise where the heap may have been of
  123.     ** required size, but a large scrap was loaded which left too little
  124.     ** memory.  To check for this, call PurgeSpace and compare the result with
  125.     ** a value that you have determined is the minimum amount of free memory
  126.     ** your application needs at initialization.  This number can be derived
  127.     ** several different ways.  One way that is fairly straightforward is to
  128.     ** run the application in the minimum size configuration as described
  129.     ** previously.  Call PurgeSpace at initialization and examine the value
  130.     ** returned.  However, you should make sure that this result is not being
  131.     ** modified by the scrap’s presence.  You can do that by calling ZeroScrap
  132.     ** before calling PurgeSpace.  Make sure to remove that call before
  133.     ** shipping, though. */
  134.  
  135.     PurgeSpace(&total, &contig);
  136.     if (total < minSpace)
  137.         DeathAlert(rBadNewsStrings, sNoFreeRoomInHeap);
  138.  
  139.     /* The extra benefit to waiting until after the Toolbox Managers have been
  140.     ** initialized to check memory is that we can now give the user an alert
  141.     ** to tell him/her what happened.  Although it is possible that the memory
  142.     ** situation could be worsened by displaying an alert, MultiFinder would
  143.     ** gracefully exit the application with an informative alert if memory
  144.     ** became critical.  Here we are acting more in a preventative manner to
  145.     ** avoid future disaster from low-memory problems. */
  146.  
  147.     if (init2)
  148.         (*(callBack)init2)();    /* Give app a chance to do some init stuff here. */
  149.  
  150.     StandardMenuSetup(rMenuBar, mApple);
  151.  
  152.     qd.randSeed = TickCount();
  153. }
  154.  
  155.  
  156.  
  157. /*****************************************************************************/
  158.  
  159.  
  160.  
  161. /* This function handles the documents selected in the finder, either for
  162. ** loading or for printing.  This is only if we don't have AppleEvents.
  163. ** If we have AppleEvents, then this will all be done automatically via
  164. ** those wonderful AppleEvent thingies. */
  165.  
  166. #pragma segment Initialize
  167. void    StartDocuments(void)
  168. {
  169.     OSErr        err;
  170.     short        i;
  171.     short        whatToDo;
  172.     short        numberOfFiles;
  173.     AppFile        theAppFile;
  174.     FSSpec        fileSpec;
  175.     long        ignore;
  176.     FileRecHndl    frHndl;
  177.     WindowPtr    docWindow;
  178.  
  179.     if (gNoDefaultDocument) return;
  180.  
  181.     err = noErr;
  182.  
  183.     if (!gHasAppleEvents) {
  184.  
  185.         CountAppFiles(&whatToDo, &numberOfFiles);
  186.  
  187.         if (numberOfFiles > 0) {
  188.  
  189.             for (i = 1; (i <= numberOfFiles) && (!err); ++i) {
  190.                 GetAppFiles(i, &theAppFile);
  191.                 ClrAppFiles(i);
  192.                 err = GetWDInfo(theAppFile.vRefNum, &fileSpec.vRefNum, &fileSpec.parID, &ignore);
  193.  
  194.                 if (err)
  195.                     HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  196.  
  197.                 else {
  198.                     pcpy(fileSpec.name, theAppFile.fName);
  199.                     err = OpenDocument(&frHndl, &fileSpec, fsRdWrPerm);
  200.                     if (!err) {
  201.                         gPrintPage = whatToDo;
  202.                             /* Open the window off-screen if we are printing. */
  203.                         err = DoNewWindow(frHndl, &docWindow, FrontWindow(), (WindowPtr)-1);
  204.                         if (err)
  205.                             DisposeDocument(frHndl);
  206.                         else {
  207.                             if (gPrintPage) {
  208.                                 err = PrintDocument(frHndl, (i == 1), (i == 1));
  209.                                 DisposeDocument(frHndl);
  210.                                 DisposeAnyWindow(docWindow);
  211.                             }
  212.                         }
  213.                         gPrintPage = 0;
  214.                     }
  215.                     if ((err) && (err != userCanceledErr)) {
  216.                         HCenteredAlert(rErrorAlert, nil, (ModalFilterProcPtr)AlertFilter);
  217.                         break;
  218.                     }
  219.                 }
  220.             }
  221.             if (whatToDo == appPrint)
  222.                 gQuitApplication = true;
  223.         }
  224.         else {
  225.             err = NewDocument(&frHndl, gTypeList[0], true);
  226.             if (!err)
  227.                 err = DoNewWindow(frHndl, nil, FrontWindow(), (WindowPtr)-1);
  228.                 if (err)
  229.                     DisposeDocument(frHndl);
  230.         }
  231.     }
  232.  
  233.     DonePrinting();        /* Clean up after printing, if we did any. */
  234. }
  235.  
  236.  
  237.  
  238.